package org.jeecg.modules.controller.articleManagement.articleManagementController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.articleManagement.entity.ArticleContent;
import org.jeecg.common.modules.articleManagement.entity.ArticleDirectory;
import org.jeecg.common.modules.articleManagement.entity.ArticleManagement;
import org.jeecg.common.modules.articleManagement.service.IArticleContentService;
import org.jeecg.common.modules.articleManagement.service.IArticleDirectoryService;
import org.jeecg.common.modules.articleManagement.service.IArticleManagementService;
import org.jeecg.common.modules.dic.entity.DicBeGoodAt;
import org.jeecg.common.modules.dic.service.IDicBeGoodAtService;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.entity.DoctorExtend;
import org.jeecg.common.modules.doctor.service.IDoctorExtendService;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 文章管理
 * @Author: jeecg-boot
 * @Date: 2023-04-21
 * @Version: V1.0
 */
@Api(tags = "文章管理")
@RestController
@RequestMapping("/api/v1/articleManagement")
@Slf4j
public class ArticleManagementController extends JeecgController<ArticleManagement, IArticleManagementService> {
    @Autowired
    private IArticleDirectoryService articleDirectoryService;
    @Autowired
    private IArticleContentService articleContentService;
    @Autowired
    public IDoctorService doctorService;
    @Autowired
    private IDoctorExtendService doctorExtendService;
    @Autowired
    private IDicBeGoodAtService dicBeGoodAtService;

    /**
     * 分页列表查询
     *
     * @param articleDirectory
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "文章目录-分页列表查询")
    @ApiOperation(value = "文章目录-分页列表查询", notes = "文章目录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ArticleDirectory articleDirectory,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ArticleDirectory> queryWrapper = QueryGenerator.initQueryWrapper(articleDirectory, req.getParameterMap());
        Page<ArticleDirectory> page = new Page<ArticleDirectory>(pageNo, pageSize);
        queryWrapper.orderByDesc("create_time");
        IPage<ArticleDirectory> pageList = articleDirectoryService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "文章内容-通过id查询")
    @ApiOperation(value = "文章内容-通过id查询", notes = "文章内容-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        ArticleContent articleContent = articleContentService.getById(id);
        ArticleContentVO articleContentVO = new ArticleContentVO();
        if (articleContent == null) {
            return Result.error("未找到对应数据");
        }
        BeanUtils.copyProperties(articleContent, articleContentVO);
        //临时使用
//        String doctorId = "161ffd804ffe41e78229ca621024259c";
        articleContentVO.setDoctorId(articleContent.getDoctorId());
        Doctor doctor = doctorService.getById(articleContentVO.getDoctorId());
        articleContentVO.setDoctor(doctor.getName());
        articleContentVO.setHeadImg(doctor.getHeadImg());
        DoctorExtend doctorExtend = doctorExtendService.getByDoctorId(articleContentVO.getDoctorId());
        articleContentVO.setGoodDesc(doctorExtend.getGoodDesc());
        articleContentVO.setIntroductionTitle(doctorExtend.getIntroductionTitle());
        DicBeGoodAt departMent = dicBeGoodAtService.getById(doctor.getDepartment());
        articleContentVO.setDepartment(departMent.getName());
        articleContentVO.setMaster(doctor.getTitle());
        return Result.ok(articleContentVO);
    }

    private String getMaster(Integer master) {
        String ma = "";
        switch (master) {
            case 1:
                ma = "国医大师";
                break;
            case 2:
                ma = "全国名中医";
                break;
            case 3:
                ma = "国家级名老中医";
                break;
            case 4:
                ma = "市级名老中医";
                break;
            case 6:
                ma = "首都国医名师";
                break;
        }
        return ma;
    }
}
